"""
    Administration application
    Handles events used for PartnerGate Administration
    (ex" /totals/partner_id)
"""
from flask import g, Blueprint
from decorators import gate_response
from models import DataAccess

admin = Blueprint('admin', __name__)


@admin.route("/total/<int:partner_id>", methods=['GET'], strict_slashes=False)
@gate_response(expires="+1h", max_age=3600)
def total(partner_id):
    """Returns the total number of urls present for a given partner_id.

       Examples:
         1. http://buzzfeed/total/50005/ => 2, 200 OK"""
    total = g.db.get_total(partner_id)
    return str(total) if total > 0 else "0"

@admin.before_request
def before_request():
    """Make sure we have access to the datamodel before each request."""
    g.db = DataAccess()
    pass

@admin.after_request
def after_request(response):
    """Make sure we cleanup after each request."""
    g.db.close()
    return response